<template>
  <div>
    <el-card class="box-card" >
      <div slot="header" class="clearfix">
        <span>手机号登录</span>
      </div>
      <div class="text item">
       <el-form :model="user" label-width="80px">
         <el-form-item label="手机号">
           <el-input v-model="user.tel"></el-input>
         </el-form-item>
         <el-form-item label="验证码">
           <el-input v-model="user.code"></el-input>
           <el-button @click="getCode">获取验证码</el-button>
         </el-form-item>
         <el-button @click="login">登录</el-button>
       </el-form>
      </div>
    </el-card>
  </div>
</template>

<script>
import { v4 as uuidv4 } from 'uuid'
export default {
  name: 'HomeView',
  data () {
    return {
      user: {
        tel: '',
        code: '',
        codeKey: ''
      }
    }
  },
  methods: {
    getCode () {
      this.user.codeKey = uuidv4()
      this.axios.post('/user/getSmsCode', this.user).then(res => {
        this.$message.success('获取验证码成功')
      })
    },
    login () {
      this.axios.post('/user/mylogin', this.user).then(res => {
        if (res.data.code === 200) {
          this.$message.success('登录成功')
          this.$store.commit('setToken', res.data.token)
        } else {
          this.$message.error(res.data.errInfo)
        }
      })
    }
  }

}
</script>
<style>
.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both
}

.box-card {
  margin-top: 10%;
  margin-left: 35%;
  width: 480px;
}
</style>
